/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/<a href="https://www.kaaction.com">KAA</a>"><a href="https://www.kaaction.com">KAA</a></a> All rights reserved.
 */
package com.kaaction.modules.ka.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kaaction.common.persistence.Page;
import com.kaaction.common.service.CrudService;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.ka.entity.MaterialDetails;
import com.kaaction.modules.ka.entity.ProductManage;
import com.kaaction.modules.ka.entity.ProductManageDetails;
import com.kaaction.modules.ka.entity.Statistics;
import com.kaaction.modules.ka.dao.ProductInstockDetailDao;
import com.kaaction.modules.ka.dao.ProductManageDao;

/**
 * 货品管理Service
 * @author huanghaiming
 * @version 2017-04-25
 */
@Service
@Transactional(readOnly = true)
public class ProductManageService extends CrudService<ProductManageDao, ProductManage> {
	
	@Autowired
	ProductInstockDetailDao productInstockDetailDao;
	
	public ProductManage get(String id) {
		return super.get(id);
	}
	
	public List<ProductManage> findList(ProductManage productManage) {
		return super.findList(productManage);
	}
	
	public Page<ProductManage> findPage(Page<ProductManage> page, ProductManage productManage) {
		return super.findPage(page, productManage);
	}
	
	@Transactional(readOnly = false)
	public void save(ProductManage productManage) {
		
		String id = productManage.getId();
		if(StringUtils.isBlank(id)){
			List<ProductManageDetails> proManDtlList =productManage.getEventSkuList();
			
			for(ProductManageDetails proManDtl:proManDtlList){
				
				productManage.preInsert();
				productManage.setSizeId(proManDtl.getSizeId());//尺码Id
				productManage.setColorId(proManDtl.getColorId());//颜色Id
				productManage.setNum(proManDtl.getNum());//数量
				productManage.setPrice(proManDtl.getPrice());//价格
				productManage.setGridNo(proManDtl.getGridNo());//库位
				productManage.setRemarks(proManDtl.getRemarks());//备注
				productManage.setSalesPrice(proManDtl.getSalesPrice());
				super.dao.insert(productManage);
				productManage.setState("0");
				super.dao.insertProInstockDtl(productManage);
			}
			
		}else{
			productManage.preUpdate();
			super.dao.update(productManage);
		}
		
	}
	
	@Transactional(readOnly = false)
	public void delete(ProductManage productManage) {
		super.delete(productManage);
	}

	@Transactional(readOnly = false)
	public void deleteAll(ProductManage productManage) {
		String[] ids = productManage.getIds();
		for(String id : ids){
			productManage.setId(id);
			super.delete(productManage);
		}
		
	}

	public List<ProductManage> findProductManage(ProductManage productManage) {
		return super.findList(productManage);
	}

	
	public int getCount(ProductManage productManage) {
		return super.dao.getCount(productManage);
	}

	@Transactional(readOnly = false)
	public void putawayProcess(ProductManage productManage) {
		
		String productId = productManage.getId();
		
		String id = super.dao.getEqualProductId(productManage);//是否存在相同状态货品
		
		if(id==null || "".equals(id)){
			productManage.preInsert();
			super.dao.insertProduct(productManage);
			productManage.setId(productId);
		}else{
			productManage.setId(id);
			super.dao.updateProduct(productManage);
			productManage.setId(productId);
		}
		
		int amount = productManage.getNum();
		int cnt = super.dao.getAmount(productManage);
		if(amount==cnt){//质检数量与质检完成数量是否一致
			super.dao.deleteId(productManage);
		}else{
			super.dao.updateId(productManage);
		}
		
		
	}

	@Transactional(readOnly = false)
	public void outboundInspec(ProductManage productManage) {
		
		int amount = productManage.getNum();
		int cnt = super.dao.getAmount(productManage);
		//需出库数量与库存出库数量是否一致
		if(amount==cnt){
			super.dao.deleteId(productManage);
		}else{
			super.dao.updateId(productManage);
		}
		productManage.preInsert();
		super.dao.insertroductOut(productManage);
		
		List<MaterialDetails> materialList = productManage.getEventMaterialList();
		for(MaterialDetails md:materialList){
			productManage.setMaterialAmount(md.getAmount());
			productManage.setMaterialDetails(md.getId());
			super.dao.updateMaterialDetails(productManage);
		}
		
		
		
	}

	@Transactional(readOnly = false)
	public void batchPutaway(ProductManage productManage,String state,String flag) {
	
		String[] ids =  productManage.getIds();
		
		for(String id:ids){
			
			ProductManage pm = super.dao.getProductManage(id,flag);//需质检上架商品
			
			if(pm!=null){
				
				pm.setState(state);
				String productId = super.dao.getEqualProductId(pm);//是否存在相同状态货品
				
				if(productId==null ||"".equals(productId)){
					pm.preInsert();
					super.dao.insertProduct(pm);
				}else{
					pm.setId(productId);
					super.dao.updateProduct(pm);
				}
				
				pm.setId(id);
				super.dao.deleteId(pm);
			}
		}
	}

	public Map<String, Object> checkQuality(String idss, String state,String msg) {
		
		Map<String,Object> map = new HashMap<String,Object>();
		String[] ids = idss.split(",");
		int a = 0;
		for(String id:ids){
			int cnt = super.dao.selectCount(id,state);//判断是否符合状态的货品
			if(cnt>0){
				a++;
				break;
			}
		}
		if(a>0){
			map.put("code", "fail");
			map.put("msg", msg);
			return map;
		}else{
			map.put("code", "success");
			map.put("msg", "成功");
			return map;
		}
	}

	@Transactional(readOnly = false)
	public void batchQualityOut(ProductManage productManage) {
		
		List<ProductManage> pms = productManage.getColumnList();
		
		for(ProductManage pm :pms){
			
			int amount = pm.getAmount();
			int cnt = super.dao.getAmount(pm);
			//需出库数量与库存出库数量是否一致
			if(amount==cnt){
				super.dao.deleteId(pm);
			}else{
				pm.setNum(pm.getAmount());
				super.dao.updateId(pm);
			}
			
			
			pm.preInsert();
			super.dao.insertroductOut(pm);
			
			List<MaterialDetails> materialList = productManage.getEventMaterialList();
			for(MaterialDetails md:materialList){
				productManage.setMaterialAmount(md.getAmount());
				productManage.setMaterialDetails(md.getId());
				super.dao.updateMaterialDetails(productManage);
			}
		}
		
	}

	public List<ProductManage> selectBatchOut(ProductManage productManage) {
		String[] ids =  productManage.getIds();
		List<String> list = new ArrayList<String>();
		
		for(String id:ids){
			list.add(id);
		}
		return super.dao.selectBatchOut(list);
	}

	@Transactional(readOnly = false)
	public void batchSoldOut(ProductManage productManage) {
		
		List<ProductManage> pms = productManage.getColumnList();
		
		for(ProductManage pm :pms){
				pm.setState("3");
				putawayProcess(pm);
		}
	}

	public List<ProductManage> selectBatchSoldOut(ProductManage productManage) {
		String[] ids =  productManage.getIds();
		List<String> list = new ArrayList<String>();
		
		for(String id:ids){
			list.add(id);
		}
		return super.dao.selectBatchSoldOut(list);
	}

	public Statistics selStatistics(ProductManage productManage) {
		return super.dao.selStatistics(productManage);
	}
}